package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author ZhangXiXi
 * @Description TODO
 * @date 2021/1/9 12:27
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @DubboReference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemsIds) {
        log.info("[检查组-添加]checkGroup:{},checkitemIds:{}", checkGroup, checkitemsIds);
        //RPC请求添加
        checkGroupService.add(checkGroup, checkitemsIds);
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/pageQuery")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查组-查询]data:{},queryPageBean:{}");
        //RPC请求添加
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-编辑]checkGroup:{},checkitemIds:{}", checkGroup, checkitemIds);
        //RPC请求添加
        checkGroupService.edit(checkGroup, checkitemIds);
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[检查组-根据id查询]id:{}", id);
//RPC请求
        CheckGroup checkGroup = checkGroupService.findById(id);

        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查组-根据检查组ID查询关联的检查项ID集和]id:{}", id);
//RPC调用业务处理
        List<Integer> itemids = checkGroupService.findCheckItemIdsByCheckGroupId(id);
//封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, itemids);
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[检查组-根据检查组ID删除]id:{}", id);
//RPC调用业务处理
        return checkGroupService.delete(id);
    }
    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
         //RPC调用业务处理
        List<CheckGroup> checkGroups=checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
}
